package com.eco.fanliapp.admin.controller.web;


import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppCompany;
import com.eco.fanliapp.entity.AppGeneralFeeScheme;
import com.eco.fanliapp.service.IAppCompanyService;
import com.eco.fanliapp.service.IAppGeneralfeeschemeService;
import com.eco.fanliapp.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AppGeneralfeeschemeController extends BaseController {

    @Autowired
    private IAppGeneralfeeschemeService generalfeeschemeService;

    @Autowired
    private IAppCompanyService appCompanyService;

    /**
     * 联盟授权地址
     * @param appGeneralfeescheme
     * @return
     */
    @SysLogAnnotation(value = "分佣方案保存",userType = 2)
    @RequestMapping(method = RequestMethod.POST, value = "/web/appGeneralfeescheme/add")
    public ServerResponse add(@RequestBody AppGeneralFeeScheme appGeneralfeescheme) {
        AppGeneralFeeScheme feeScheme = new AppGeneralFeeScheme();
        feeScheme.setSchemeName(appGeneralfeescheme.getSchemeName());
        int size = generalfeeschemeService.selectAppGeneralfeeschemeList(feeScheme).size();
        if(size > 1){
            return ServerResponse.createByErrorMessage("方案名称重名，请重新输入");
        }
        if(getAuthentication().getRoleId() != 0){
            appGeneralfeescheme.setCompanyId(getAuthentication().getCompanyId());
        }
        int i = generalfeeschemeService.insertAppGeneralfeescheme(appGeneralfeescheme);
        if (appGeneralfeescheme.getGeneralFeeSchemeId() != null) {
            //刷新redis
            generalfeeschemeService.selectAppGeneralfeeschemeById(appGeneralfeescheme.getGeneralFeeSchemeId(), 0);
        }
        if(i>0){
            return ServerResponse.createBySuccessMessage("分佣方案保存成功！");
        }
        return ServerResponse.createByErrorMessage("分佣方案保存失败");
    }

    @SysLogAnnotation(value = "删除分佣方案",userType = 2)
    @RequestMapping(method = RequestMethod.GET, value = "/web/appGeneralfeescheme/remove")
    public ServerResponse remove(Integer id) {

        int i = generalfeeschemeService.delete(id);
        if(i>0){
            return ServerResponse.createBySuccessMessage("分佣方案删除成功！");
        }
        return ServerResponse.createByErrorMessage("分佣方案删除失败");
    }

    /**
     * 分佣列表
     * @param currentPage
     * @param pageSize
     * @return
     */

    @RequestMapping(method = RequestMethod.GET, value = "/web/appGeneralfeescheme/findGeneralfeeschemeAll")
    public ServerResponse<PageInfo> findGeneralfeeschemeAll(@RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Integer companyId = getAuthentication().getCompanyId();
        if(companyId == 1){
            companyId = null;
        }
        AppGeneralFeeScheme generalFeeScheme = new AppGeneralFeeScheme();
        generalFeeScheme.setCompanyId(companyId);
        List<AppGeneralFeeScheme> appGeneralFeeSchemes = generalfeeschemeService.selectAppGeneralfeeschemeList(generalFeeScheme);
        if (appGeneralFeeSchemes == null){
            return ServerResponse.createByErrorMessage("查询失败");
        }

        return ServerResponse.createBySuccess(companyId==null?"1":"查询成功",new PageInfo(appGeneralFeeSchemes));
    }


}
